<template>
    <el-dialog
      :visible.sync="visible"
      :title="title"
      :width="dialogWidth"
      :close-on-click-modal="false"
      :before-close="handleClose"
      class="phone-update-modal"
    >
      <el-form
        ref="phoneForm"
        :model="phoneForm"
        :rules="phoneRules"
        label-width="100px"
        class="phone-form"
      >
        <!-- 原手机号输入框 -->
        <el-form-item label="原手机号" prop="oldPhone">
          <el-input
            v-model="phoneForm.oldPhone"
            type="text"
            placeholder="请输入原手机号"
            @keyup.enter.native="handleSubmit"
          ></el-input>
        </el-form-item>
  
        <!-- 新手机号输入框 -->
        <el-form-item label="新手机号" prop="newPhone">
          <el-input
            v-model="phoneForm.newPhone"
            type="text"
            placeholder="请输入新手机号"
            @keyup.enter.native="handleSubmit"
          ></el-input>
        </el-form-item>
  
        <!-- 密码输入框（仅验证是否输入） -->
        <el-form-item label="登录密码" prop="password">
          <el-input
            v-model="phoneForm.password"
            type="password"
            placeholder="请输入登录密码"
            :show-password="showPassword"
            @keyup.enter.native="handleSubmit"
          >
            <i
              slot="suffix"
              class="el-icon-view"
              @click="showPassword = !showPassword"
              :class="{ 'active': showPassword }"
            ></i>
          </el-input>
        </el-form-item>
      </el-form>
  
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-dialog>
  </template>
  
  <script>
  export default {
    name: 'PhoneUpdateModal',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: '修改手机号'
      },
      dialogWidth: {
        type: String,
        default: '500px'
      }
    },
    data() {
      // 验证手机号格式
      const validatePhoneFormat = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入手机号'));
          return;
        }
        
        // 手机号正则验证（11位数字，以1开头）
        const reg = /^1[3-9]\d{9}$/;
        if (!reg.test(value)) {
          callback(new Error('请输入正确的手机号'));
          return;
        }
        
        callback();
      };
  
      // 验证新手机号与原手机号是否不同
      const validateNewPhone = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入新手机号'));
          return;
        }
        
        // 先验证格式
        const reg = /^1[3-9]\d{9}$/;
        if (!reg.test(value)) {
          callback(new Error('请输入正确的手机号'));
          return;
        }
        
        // 检查是否与原手机号相同
        if (value === this.phoneForm.oldPhone) {
          callback(new Error('新手机号不能与原手机号相同'));
          return;
        }
        
        callback();
      };
  
      return {
        // 表单数据
        phoneForm: {
          oldPhone: '',
          newPhone: '',
          password: ''
        },
        // 表单验证规则（密码仅验证必填）
        phoneRules: {
          oldPhone: [
            { required: true, validator: validatePhoneFormat, trigger: 'blur' }
          ],
          newPhone: [
            { required: true, validator: validateNewPhone, trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' }
          ]
        },
        // 控制密码显示/隐藏
        showPassword: false
      };
    },
    methods: {
      /**
       * 提交表单
       */
      handleSubmit() {
        this.$refs.phoneForm.validate(valid => {
          if (valid) {
            // 表单验证通过，提交数据
            console.log('修改手机号数据:', this.phoneForm);

            this.$emit('submit', {
                oldPhone: this.phoneForm.oldPhone,
                newPhone: this.phoneForm.newPhone,
                password: this.phoneForm.password,
            });
          }
        });
      },
      
      /**
       * 取消操作
       */
      handleCancel() {
        this.$emit('cancel');
        this.resetForm();
      },
      
      /**
       * 关闭弹窗前的操作
       */
      handleClose() {
        this.$emit('update:visible', false);
        this.resetForm();
      },
      
      /**
       * 重置表单
       */
      resetForm() {
        if (this.$refs.phoneForm) {
          this.$refs.phoneForm.resetFields();
        }
        this.showPassword = false;
      }
    },
    watch: {
      // 监听弹窗显示状态，关闭时重置表单
      visible(val) {
        if (!val) {
          this.resetForm();
        }
      }
    }
  };
  </script>
  
  <style scoped>
  .phone-form {
    margin-top: 10px;
  }
  
  /* 密码可见图标样式 */
  ::v-deep .el-icon-view {
    cursor: pointer;
    color: #c0c4cc;
    transition: color 0.2s;
  }
  
  ::v-deep .el-icon-view.active {
    color: #409eff;
  }
  </style>
      